package servlet;

<<<<<<< HEAD
import Dao.LoginDao;
import beans.Staff;
import java.io.IOException;
=======
>>>>>>> f693e67 (代码)
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
<<<<<<< HEAD

@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
	private final static String p1 = "员工";
	private final static String p2 = "经理";
	private final static String p3 = "财务";
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String sno = (String)request.getParameter("sno");
        String pwd = (String)request.getParameter("password");
        String position = (String)request.getParameter("position");
        Staff  staff = new Staff(sno,pwd,position);
        int result = LoginDao.login(staff);
        if(result>0)
        {
        	if(staff.getPosition().equals(p1)) {
        		request.getRequestDispatcher("staff.jsp").forward(request,response);
        	}else if(staff.getPosition().equals(p2)) {
        		request.getRequestDispatcher("manage.jsp").forward(request,response);
        	}else if(staff.getPosition().equals(p3)){
        		request.getRequestDispatcher("finance.jsp").forward(request,response);
        	}
        }else {
            response.sendRedirect("login.jsp");
        }
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	}

=======
import javax.servlet.http.HttpSession;
import java.io.IOException;

import beans.Account;
import service.*;

@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8;");
        doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8;");
        HttpSession session = request.getSession();

        String sno = request.getParameter("sno");
        String password = request.getParameter("password");

        AccountService accountService = new AccountService();
        Account account = accountService.getAccount(sno);

        if (account == null){
            response.sendRedirect("login.jsp?error=0");
        } else {
            if (account.getAccountState() == 0) {
                response.sendRedirect("login.jsp?error=1");
            } else {
            	System.out.print(account.getPassWord());
            	System.out.print(password);
                if (account.getPassWord().equals(password)) {
                	session.setAttribute("account",account);
                	if(account.getPosition().equals("经理")) {
                		response.getWriter().write("<script language='javascript'>alert('欢迎" + account.getAccountName() + "经理');" +
                                   "window.location.href='Manage/index.jsp';</script>");
                	}else if(account.getPosition().equals("员工")){
                		response.getWriter().write("<script language='javascript'>alert('欢迎" + account.getAccountName() + "员工');" +
                                    "window.location.href='Staff/index.jsp';</script>");
                	}else {
                		response.getWriter().write("<script language='javascript'>alert('欢迎 ！" + account.getAccountName() + "员工');" +
                                "window.location.href='Finance/index.jsp';</script>");
                	}
                } else {
                    response.sendRedirect("login.jsp?error=2");
                }
            }
        }
    }
>>>>>>> f693e67 (代码)
}
